package stackqueue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/17 14:36
 * https://leetcode-cn.com/problems/next-greater-element-i/
 */
public class NextGreaterElement {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length];
        Map<Integer, Integer> hash = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            hash.put(nums1[i], i);
        }
        Arrays.fill(result,-1);
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for (int i = 1; i < nums2.length; i++) {
            while (!stack.isEmpty() && nums2[i] > nums2[stack.peek()]) {
                if (hash.containsKey(nums2[stack.peek()])) {
                    result[hash.get(nums2[stack.peek()])] = nums2[i];
                }
                stack.pop();
            }
            stack.push(i);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] t = {73, 74, 75, 71, 69, 72, 76, 73};

    }
}
